package com.gongye.courseselectionsystem.schedule;

import com.alibaba.excel.EasyExcel;
import com.gongye.courseselectionsystem.mapper.ChooseCourseMapper;
import com.gongye.courseselectionsystem.mapper.ChooseCourseVoMapper;
import com.gongye.courseselectionsystem.mapper.StudentAmountForTeacherVoMapper;
import com.gongye.courseselectionsystem.pojo.po.ChooseCourse;
import com.gongye.courseselectionsystem.pojo.po.Student;
import com.gongye.courseselectionsystem.pojo.vo.ChooseCourseStudent;
import com.gongye.courseselectionsystem.pojo.vo.ChooseCourseVo;
import com.gongye.courseselectionsystem.pojo.vo.StudentAmountForTeacherVo;
import com.gongye.courseselectionsystem.service.QueryCourseService;
import com.gongye.courseselectionsystem.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author baorongchuan
 */
@Component
public class SendExcelTeacherScheduleTask {
    @Resource
    private ChooseCourseMapper chooseCourseMapper;
    @Resource
    private ChooseCourseVoMapper chooseCourseVoMapper;
    @Resource
    private QueryCourseService queryCourseService;
    @Resource
    private MailUtil mailUtil;
    @Resource
    private StudentAmountForTeacherVoMapper studentAmountForTeacherVoMapper;
    private final String email = "2388464363@qq.com";
    private final String time = "0 15 17 4 11 ?";

    @Autowired
    /**@Scheduled(cron = "0 0/5 8-16 * * MON-Fri")*/
    public void sendAllChoose() {
        //创建一个选课文档
        String fileName = "src/main/resources/xls/选课详情.xls";
        List<ChooseCourse> chooseCourses = queryCourseService.queryAll();
        //设置排出的属性
        Set<String> set = new HashSet<>();
        set.add("id");
        EasyExcel.write(fileName, ChooseCourse.class).excludeColumnFiledNames(set).sheet("已选的课学生").doWrite(chooseCourses);
        mailUtil.sendAttachmentMail(email, "已选课的学生信息", "  星哥，好！<br><br>&emsp; 附件是所有已经选课的学生信息。<br><br><br><br><br><br>祝，工作顺利！", "D:\\ideaProjects\\course-selection-system\\src\\main\\resources\\xls\\选课详情.xls");
    }

    @Autowired
    @Scheduled(cron = "0 0/5 8-16 * * MON-Fri")
    public void sendAllChooseStudent() {
        //创建一个选课文档
        String fileName = "src/main/resources/xls/学生选课详情.xls";
        List<ChooseCourseStudent> chooseCourseStudent = queryCourseService.queryChooseCourseStudent();
        EasyExcel.write(fileName, ChooseCourseStudent.class).sheet("已选的课学生").doWrite(chooseCourseStudent);
        mailUtil.sendAttachmentMail(email, "已选课的学生信息", "  星哥，好！<br><br>&emsp; 附件是所有已经选课的学生信息。<br><br><br><br><br><br>祝，工作顺利！", "D:\\ideaProjects\\course-selection-system\\src\\main\\resources\\xls\\学生选课详情.xls");
    }

    @Scheduled(cron = time)
    @Autowired
    public void sendChooseVo() {
        //创建一个每名老师对应的选课学生表
        String fileName = "src/main/resources/xls/每名老师对应的学生表.xls";
        List<ChooseCourseVo> chooseCourseVos = chooseCourseVoMapper.teacherHaveStudent();
        EasyExcel.write(fileName, ChooseCourseVo.class).sheet("每名老师对应的学生").doWrite(chooseCourseVos);
        mailUtil.sendAttachmentMail(email, "每名老师对应的学生信息", "星哥,好！<br><br>&emsp;附件是每名老师对应的学生信息。<br><br><br><br><br><br>祝，工作顺利！", "D:\\ideaProjects\\course-selection-system\\src\\main\\resources\\xls\\每名老师对应的学生表.xls");
    }

    @Scheduled(cron = time)
    @Autowired
    public void sentStudentAmount() {
        //创建一个老师每门课对应的学生数量
        String fileName = "src/main/resources/xls/老师每门课对应的学生数量.xls";
        List<StudentAmountForTeacherVo> studentAmountForTeacherVo = studentAmountForTeacherVoMapper.studentAmount();
        EasyExcel.write(fileName, StudentAmountForTeacherVo.class).sheet("每名老师对应的学生").doWrite(studentAmountForTeacherVo);
        mailUtil.sendAttachmentMail(email, "老师每门课对应的学生数量", "星哥,好!<br><br>&emsp; 附件是老师每门课对应的学生数量。<br><br><br><br><br><br>祝，工作顺利！", "D:\\ideaProjects\\course-selection-system\\src\\main\\resources\\xls\\老师每门课对应的学生数量.xls");

    }


    @Scheduled(cron = time)
    @Autowired
    public void sentNoChooseCourse() {
        //创建一个没有选课的学生表
        String fileName = "src/main/resources/xls/没有选课的学生表.xls";
        List<Student> noChooseCourse = chooseCourseMapper.getNoChooseCourse();
        Set<String> set = new HashSet<>();
        set.add("id");
        EasyExcel.write(fileName, Student.class).excludeColumnFiledNames(set).sheet("每名老师对应的学生").doWrite(noChooseCourse);
        mailUtil.sendAttachmentMail(email, "没有选课的学生", "星哥,好!<br><br>&emsp;附件是没有选课的学生。<br><br><br><br><br><br>祝，工作顺利！", "D:\\ideaProjects\\course-selection-system\\src\\main\\resources\\xls\\没有选课的学生表.xls");
    }
}
